package com.ymatou.iapi.productmanager.testcase;

import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.parameter.productCheckStatus.CheckStatusDto;
import com.ymatou.iapi.productmanager.parameter.productCheckStatus.UpdateProductCheckStatusRequest;
import com.ymatou.iapi.productmanager.service.UpdateProductCheckStatusCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.sqlwapper.YmtLiveWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Ts_UpdateProductCheckStatus {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static IntergratedProductMapper intergratedProductMapper = SqlMapperFactory.getIntergratedProductMapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新商品的审核状态");
		intergratedProductMapper.updateProductoffSale(sellerId);
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new UpdateProductCheckStatusCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateProductCheckStatus_001() {
		Logger.start(true, "上架商品 强制下架");
		try {
			
			AddSellerProductRequest addProdReq1 = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			AddSellerProductRequest addProdReq2 = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			addProdReq1.getProduct().setOnSaleType(0);
			addProdReq2.getProduct().setOnSaleType(0);
			String productId1 = ProductManagerCallServiceV3.AddSellerProduct(addProdReq1);
			String productId2 = ProductManagerCallServiceV3.AddSellerProduct(addProdReq2);

//			String productId1 = ProductManagerCallServiceV2.addNewProductOnSale(addProdReq1).get("Data").getAsJsonObject().get("ProductId").getAsString();
//			String productId2 = ProductManagerCallServiceV2.addNewProductOnSale(addProdReq2).get("Data").getAsJsonObject().get("ProductId").getAsString();

			
			// 强制下架
			UpdateProductCheckStatusRequest requestBean = getUpdateProductCheckStatusRequest(MapUtil.hashMap(sellerId, productId1, 3383, productId2), -1);

			UpdateProductCheckStatusCall upcall = new UpdateProductCheckStatusCall();
			upcall.setData(requestBean);
			upcall.callService();
			String ret = upcall.getReturnData();

			checkUpdateProductCheckStatus(requestBean, ret);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateProductCheckStatus_002() {
		Logger.start(true, "上架商品 强制下架后,强制上架");
		try {
			AddSellerProductRequest addProdReq1 = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			AddSellerProductRequest addProdReq2 = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			addProdReq1.getProduct().setOnSaleType(0);
			addProdReq2.getProduct().setOnSaleType(0);
			String productId1 = ProductManagerCallServiceV3.AddSellerProduct(addProdReq1);
			String productId2 = ProductManagerCallServiceV3.AddSellerProduct(addProdReq2);
//			String productId1 = ProductManagerCallServiceV2.addNewProductOnSale(addProdReq1).get("Data").getAsJsonObject().get("ProductId").getAsString();
//			String productId2 = ProductManagerCallServiceV2.addNewProductOnSale(addProdReq2).get("Data").getAsJsonObject().get("ProductId").getAsString();

			// 强制下架
			UpdateProductCheckStatusRequest requestBean = getUpdateProductCheckStatusRequest(MapUtil.hashMap(sellerId, productId1, 3383, productId2), -1);

			UpdateProductCheckStatusCall upcall = new UpdateProductCheckStatusCall();
			upcall.setData(requestBean);
			upcall.callService();
			String ret = upcall.getReturnData();

			checkUpdateProductCheckStatus(requestBean, ret);

			// 恢复
			requestBean.setCheckStatus(0);
			upcall.setData(requestBean);
			upcall.callService();
			String ret2 = upcall.getReturnData();
			YMTDateUtil.waitTime(5);
			checkUpdateProductCheckStatus(requestBean, ret2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateProductCheckStatus_003() {
		Logger.start(true, "强制非该买手商品下架");
		try {
			AddSellerProductRequest addProdReq1 = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			addProdReq1.getProduct().setOnSaleType(0);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addProdReq1);
			//String productId = ProductManagerCallServiceV2.addNewProductOnSale(addProdReq1).get("Data").getAsJsonObject().get("ProductId").getAsString();

			// 强制下架
			UpdateProductCheckStatusRequest requestBean = getUpdateProductCheckStatusRequest(MapUtil.hashMap(sellerId + 1, productId), -1);

			UpdateProductCheckStatusCall upcall = new UpdateProductCheckStatusCall();
			upcall.setData(requestBean);
			upcall.callService();
			String ret = upcall.getReturnData();
			Logger.verifyEquals("ok", ret, "返回值检查");

			Map<String, Object> retInSql = intergratedProductMapper.selectProductByProdId(productId);
			Logger.verifyEquals(0, Integer.parseInt(retInSql.get("CheckStatus").toString()), "CheckStatus检查");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_UpdateProductCheckStatus_004() {
		Logger.start(true, "强制下架不存在商品");
		try {
			String productId = "123123213";

			// 强制下架
			UpdateProductCheckStatusRequest requestBean = getUpdateProductCheckStatusRequest(MapUtil.hashMap(sellerId, productId), -1);

			UpdateProductCheckStatusCall upcall = new UpdateProductCheckStatusCall();
			upcall.setData(requestBean);
			upcall.callService();
			String ret = upcall.getReturnData();
			Logger.verifyEquals("ok", ret, "返回值检查");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	public static void checkUpdateProductCheckStatus(UpdateProductCheckStatusRequest requestBean, String ret) throws Exception {
		Logger.verifyEquals("ok", ret, "返回值检查");

		List<CheckStatusDto> productList = requestBean.getProductList();
		// 强制下架检查
		if (requestBean.getCheckStatus() == -1) {
			for (CheckStatusDto ele : productList) {
				Map<String, Object> retInSql = intergratedProductMapper.selectProductByProdId(ele.getProductId());
				Logger.verifyEquals(requestBean.getOperator(), retInSql.get("Checker"), "操作人检查");
				Logger.verifyEquals(requestBean.getCheckStatus(), Integer.parseInt(retInSql.get("CheckStatus").toString()), "CheckStatus检查");
				checkOffSaleTime(productList.stream().map(x -> x.getProductId()).collect(Collectors.toList()));
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(ele.getProductId());
			}
		} else {
			// 取消强制下架检查
			for (CheckStatusDto ele : productList) {
				Map<String, Object> retInSql = intergratedProductMapper.selectProductByProdId(ele.getProductId());
				Logger.verifyEquals(requestBean.getOperator(), retInSql.get("Checker"), "操作人检查");
				Logger.verifyEquals(requestBean.getCheckStatus(), Integer.parseInt(retInSql.get("CheckStatus").toString()), "CheckStatus检查");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(ele.getProductId());
			}
		}

	}

	public static UpdateProductCheckStatusRequest getUpdateProductCheckStatusRequest(Map<Integer, String> userIdAndProdIdList, int status) {
		UpdateProductCheckStatusRequest requestBean = new UpdateProductCheckStatusRequest();
		requestBean.setOperator("autotest");
		requestBean.setCheckStatus(status);

		List<CheckStatusDto> productList = new ArrayList<>();
		userIdAndProdIdList.forEach((userId, prodId) -> {
			CheckStatusDto cs = new CheckStatusDto();
			cs.setSellerId(userId);
			cs.setProductId(prodId);
			productList.add(cs);

		});

		requestBean.setProductList(productList);
		return requestBean;
	}

	public static void checkOffSaleTime(List<String> productIdList) throws Exception {
		Map<String, Boolean> productIdAndIsInLive = MapUtil.hashMap();
		productIdList.forEach(x -> productIdAndIsInLive.put(x, false));
		checkOffSaleTime(productIdAndIsInLive);
	}

	@SuppressWarnings("rawtypes")
	public static void checkOffSaleTime(Map<String, Boolean> productIdAndIsInLive) throws Exception {
		Date nowDate = YMTDateUtil.getDateBeforeOrNextSecond(600);
		productIdAndIsInLive.forEach((productId, isInLive) -> {
			try {
				// SQL YmtProducts
				List<Map> productIDsqllist = new YmtProductsIWapper().selectProductByProductIdforMap(productId);
				Date svalidStartbysql = (Date) productIDsqllist.get(0).get("validStart");
				Date svalidEndbysql = (Date) productIDsqllist.get(0).get("validEnd");
				Logger.verifyEquals(true, svalidStartbysql.before(nowDate), "开始时间早于当前时间");
				Logger.verifyEquals(true, svalidEndbysql.before(nowDate), "结束时间早于当前时间");

				if (isInLive) {
					List<Map> liveProductMapList = new YmtProductsInLiveWapper().selectInLiveByProductId(productId);
					liveProductMapList.forEach(x -> {
						int liveId = Integer.parseInt(x.get("iActivityId").toString());

						Logger.verifyEquals(-1, Integer.parseInt(x.get("iAction").toString()),
								"下架商品，iaction应为-1 liveId:" + liveId + ", productId:" + x.get("sProductId").toString());
						try {
							// 默认下架后，直播中没有有效商品
							Map liveMap = new YmtLiveWapper().selectforMapByLiveId(liveId).get(0);
							Logger.verifyEquals(0, Integer.parseInt(liveMap.get("ProductCount").toString()), "下架后, productcount应为0,liveId:" + liveId);
							Logger.verifyEquals(0, Integer.parseInt(liveMap.get("PutawayCount").toString()), "下架后, PutawayCount应为0,liveId:" + liveId);

							NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(liveId, false);
						} catch (Exception e) {
							e.printStackTrace();
						}
					});
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		});
	}

}
